from flask import   Flask,render_template,request
import pymysql

app=Flask(__name__)
def insert(data):
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', charset='utf8',
                           db='unicom')  # 链接mysql
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    order = "insert into user(name,age,gender,email,password) values(%s,%s,%s,%s,%s);"
    cursor.execute(order, data)
    conn.commit()
    cursor.close()
    conn.close()

def select():
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', charset='utf8',
                           db='unicom')  # 链接mysql
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    order="select * from user;"
    cursor.execute(order)
    data_list=cursor.fetchall()

    cursor.close()
    conn.close()
    return data_list

@app.route('/register',methods=['GET','POST'])
def register():
    if request.method=='GET':
        return render_template('/pages/login.html')
    elif request.method=='POST':
        username=request.form.get('username')
        email=request.form.get('email')
        gender=request.form.get('gender')
        age=request.form.get('age')
        pwd=request.form.get('pwd')
        data=[username,int(age),gender,email,pwd]
        print(username,age,email,gender,pwd)
        insert(data)
        data_list = select()
        return render_template('/pages/userInfo.html', data_list=data_list)


@app.route('/show')
def show():
    data_list=select()
    return render_template('/pages/userInfo.html',data_list=data_list)


@app.route('/test')
def test():
    list=[1,2,3,4]
    return '你好'


if __name__=='__main__':
    #app.run()
    app.run(debug =True ,host='127.0.0.1', port='8001')